<template>
	<transition>
		
		<!--  #ifdef MP || APP-PLUS -->
			<view class="message-container"
				:class="show?'to':'out'" 
				:style="{top:top+'rpx',left:left}" v-if="tShow">
				<view class="avatar">
					<image :src="detail.avatar" mode="widthFix" class="image"></image>
				</view>
				<view class="title">
					{{detail.nickname}}参与了拼单
				</view>
			</view>
		<!-- #endif -->
		<!--  #ifdef H5 -->
			<view class="message-container"
				:style="{top:top+'rpx'}" v-if="show">
				<view class="avatar">
					<image :src="detail.avatar" mode="widthFix" class="image"></image>
				</view>
				<view class="title">
					{{detail.nickname}}参与了拼单
				</view>
			</view>
		<!-- #endif -->
		
	</transition>
</template>

<script>
export default{
	data(){
		return {
			tShow:false,
			left:"40rpx",
			intervalRef:null,
			timeoutRef:null,
			show:false,
			detail:{},
			index:-1
		}
	},
	mounted() {
	},
	beforeDestroy() {
		if(this.intervalRef){
			clearInterval(this.intervalRef);
		}
		if(this.timeoutRef){
			clearTimeout(this.timeoutRef)
		}
	},
	props:{
		top:{
			type:Number,
			default:120
		},
		// 
		interval:{
			type:Number,
			default:3000
		},
		handoff:{
			type:Number,
			default:1000
		},
		list:{
			type:Array,
			default:()=>[]
		}
	},
	methods:{
		out(){
			this.left = `-100%`
		},
	},
	watch:{
		// #ifdef MP || APP-PLUS
		show:{
			handler(value){
				if(value){
					this.left = `40rpx`
					this.tShow = true
				}else{
					this.out();
				}
			},
			immediate:true
		},
		// #endif
		index:{
			handler(value){
				this.show = true;
			},
		},
		list:{
			handler(value){
				if(value.length){
					this.index = 0;
					this.show = true;
					this.detail = this.list[this.index];
					const that = this;
					this.intervalRef =  setInterval(()=>{
						that.show = false;
						that.timeoutRef = setTimeout(()=>{
							that.detail = that.list[that.index%that.list.length];
							if(that.timeoutRef){
								clearTimeout(that.timeoutRef)
							}
							that.index++
						},that.handoff);
					},this.interval)
				}
			},
			immediate:true
		}
	}
	
}
</script>

<style lang="scss" scoped>
.image{
	height: 100%;
	width: 100%;
}
.message-container{
	position: absolute;
	display: flex;
	align-items: center;
	padding: 8rpx 12rpx;
	background-color: rgba(0, 0, 0, 0.6);
	border-radius: 8rpx;
	color: white;
	white-space: nowrap;
	left: 40rpx;
	transition: all 500ms ease; 

	.avatar{
		width: 40rpx;
		height: 40rpx;
		margin-right: 12rpx;
	}
}

// #ifdef MP || APP-PLUS
.message-container{
	
	animation-duration: 0.5s;
}
.to{
	animation-name: leave-to;
}
@keyframes leave-to {
  from{
	  left: 100%;
  }
  to {
    left: 40rpx;
  }
}
// #endif

// #ifdef H5
.v-enter-active,.v-leave-active{
  transition: all 0.5s ease;
}

.v-enter{
	left: 100%;
}
.v-enter-to,.v-leave{
	left: 40rpx;
}
.v-leave-to{
	left: 0%;
	transform: translate(-105%,0);
}
// #endif



</style>